package lib.XML;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import lib.Base.PublicGroups;
import lib.Base.PublicGroup;

public class PublicGroupsContentHandler implements ContentHandler {
	/**
	 * Class to handle XML content public groups
	 */
	private String currentValue;
	private PublicGroups publicGroups;
	private PublicGroup publicGroup;

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue = new String(ch, start, length);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if (localName.equals("publicGroups")) {
			publicGroups = new PublicGroups(Integer.parseInt(atts
					.getValue("lastUpdate")));
		} else if (localName.equals("group")) {
			publicGroup = new PublicGroup(atts.getValue("id"));
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("name")) {
			publicGroup.setName(currentValue);
		} else if (localName.equals("about")) {
			publicGroup.setAbout(currentValue);
		} else if (localName.equals("group")) {
			publicGroups.addPublicGroup(publicGroup);
		}
	}

	public PublicGroups getPublicGroups() {
		return publicGroups;
	}

	public void endDocument() throws SAXException {
	}

	public void endPrefixMapping(String prefix) throws SAXException {
	}

	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	public void setDocumentLocator(Locator locator) {
	}

	public void skippedEntity(String name) throws SAXException {
	}

	public void startDocument() throws SAXException {
	}

	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}
}